home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: night.primate.wisc.edu!relay!news
- From: Tom McLintock <tmclint>
- Subject: An (Ada-style) enum template class??
- Content-Type: text/plain; charset=us-ascii
- Message-ID: <1996Mar8.032801.27864@relay.nswc.navy.mil>
- Sender: news@relay.nswc.navy.mil
- Content-Transfer-Encoding: 7bit
- Organization: Naval Surface Warfare Center - Dahlgren Div.
- Mime-Version: 1.0
- Date: Fri, 8 Mar 1996 03:28:01 GMT
- X-Mailer: Mozilla 1.12IS (X11; I; IRIX 5.3 IP22)
- X-Url: news:comp.lang.c++
-
- Has anyone made a general-purpose, powerful enum "container" class? This would
- create a class containing full functionality for an enum you give it, or a list
- of enum values you give it. With a variable of such a "type", you could
-
- - read an enum value from a text file into the variable and write its value to
- the screen (not just its numeric int-value, but a string representation of
- the
- value's name)
-
- - increment and decrement the variable with ++ and --, and throw an exception
- when you go off one end of the enumerated values
-
- - get the number of enum values so you can, say, define an array indexed by
- the enum class
-
- - add values to the enum class when deriving a new class from the base enum
- class
-
- - of course use the variable as an lvalue or an rvalue that works just like a
- normal enum variable
-
- - all this without having to specify the values of the enum to the class as
- both an actual enum type and an array of string representations of the enum
- values.
-
- I've come up with solutions to some of these problems using, among other
- tricks,
- BIG #define macros and bizarre non-C/C++-looking syntax, but there MUST be a
- better way! All of the above features are useful and are in Ada (a language
- I'm
- not generally fond of otherwise). Bjarne Stroustrup seems to think that an
- enum
- shouldn't have to do all this, from what I've read, but my experiences with Ada
- suggest otherwise.
-
- BTW, I've found that the humble #define still has a lot of usefulness in C++,
- in
- spite of what many writers claim. For example, I wrote a trace macro that
- lets you write, for example,
- TRACE(i _ x _ str); // i, x, and str are int, float and char* variables
- and when the program runs, the output from the TRACE call is something like
- ###filename.C (line 234)###
- i: 42
- x: 3.14159
- str: "Hello world"
- Let's see anyone do that in C or without using the preprocessor!
-
- --
- - Tom McLintock
- . //
- ___|\____|_________//______________/|______
- ___|/_|__|)_______//|_________|\__/_|______
- __/|__|)____|____//_|___/\____|_\/__|______
- _{_|\_______|)______|___\/____|_____|______
- __\|/_____________(@)_________|___(@)______
- | (@)
-
-